﻿#include "cal.h"
#include <QButtonGroup>
#include <QDebug>
#include <QtCore/qmath.h>
cal::cal(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	initUI();
}

cal::~cal()
{

}
void cal::initUI() 
{
	//找到所有的按钮,并放到一个按钮组里面
	auto buttonGroup = new QButtonGroup(this);
	auto btnList=findChildren<QPushButton*>();
	for (auto btn : btnList) {
		buttonGroup->addButton(btn);
	}
	//关联按钮组的点击信号
	connect(buttonGroup, QOverload<QAbstractButton*>::of(&QButtonGroup::buttonClicked), this, &cal::onButtonGroupClicked);

	vec.resize(5);
	qInfo()<<"vecSize"<<vec.size()<<"vecCapacity"<<vec.capacity();
}

void cal::onButtonGroupClicked(QAbstractButton* btn) 
{
	float val = ui.line_down->text().toFloat();
	//qInfo() << btn->text();
	QString name = btn->text();
	//根据不同的按钮，来处理不同的逻辑
	//如果是数字直接显示出来
	if (name >= '0'&& name <= '9' || name == '.') {
		//删掉最开始显示的0,如果输入的是小数点，则最开始的0保留
		if (ui.line_down->text()=="0" && name != '.') {
			ui.line_down->clear();
		}
		//重置数字
		if (preBtn == "+"||preBtn=="-"||preBtn== QString(QChar(0x00D7)) ||preBtn== QString(QChar(0x00F7)))
		{
			ui.line_down->clear();
		}
		ui.line_down->insert(name);
	}
	//如果是C上下框都清除
	else if (name == "C")
	{
		ui.line_up->clear();
		ui.line_down->clear();
		ui.line_down->insert("0");
		vec.clear();
		vec.resize(5);
	}
	//如果是CE只清除下面的
	else if (name == "CE")
	{
		ui.line_down->clear();
	}
	//如果是Del删除一个字符即可
	else if (name == "Del")
	{
		ui.line_down->setCursorPosition(ui.line_down->cursorPosition() - 1);
		ui.line_down->del();
	}
	//如果是+，左操作数和操作符存到vec中
	else if (name == "+")
	{
		if (vec[2].isNull()) 
		{
			//把左操作数和操作符存到vec中
			vec[0] = val;
			vec[1] = "+";
		}
	}
	//×号的unicode编码是00D7
	else if (name == QString(QChar(0x00D7)))
	{
		if (vec[2].isNull())
		{
			//把左操作数和操作符存到vec中
			vec[0] = val;
			vec[1] = "*";
		}
	}
	//÷
	else if (name == QString(QChar(0x00F7)))
	{
		if (vec[2].isNull())
		{
			//把左操作数和操作符存到vec中
			vec[0] = val;
			vec[1] = "\\";
		}
	}
	else if (name == "-")
	{
		if (vec[2].isNull())
		{
			//把左操作数和操作符存到vec中
			vec[0] = val;
			vec[1] = "-";
		}
	}
	else if (name == "+/-")
	{
		if (vec[2].isNull())
		{
			//把左操作数和操作符存到vec中
			vec[0] = val;
			vec[1] = "neg";
		}
	}
	else if (name == "Rec")
	{
		vec[0] = 1;
		vec[1] = "/";
		vec[2] = val;
	}
	else if (name == "Squ")
	{
		vec[0] = val;
		vec[1] = "^2";
	}
	else if (name == "Sqrt")
	{
		vec[0] = val;
		vec[1] = "r";
	}
	else if (name == "%")
	{
		vec[0] = val;
		vec[1] = "%";
	}
	else if(name=="=")
	{
		//vec[0]存左操作数,vec[2]存放右操作数,vec[4]存结果
		vec[2] = val;
		vec[3] = "=";
		if (vec[1] == "+")
		{
			vec[4] = vec[0].toFloat() + vec[2].toFloat();
		}
		else if (vec[1] == "-")
		{
			vec[4] = vec[0].toFloat() - vec[2].toFloat();
		}
		else if (vec[1] == "*")
		{
			vec[4] = vec[0].toFloat() * vec[2].toFloat();
		}
		else if (vec[1] == "\\")
		{
			vec[4] = vec[0].toFloat() / vec[2].toFloat();
		}
		else if (vec[1] == "/")
		{
			vec[4] = 1.0 / vec[2].toFloat();
		}
		else if (vec[1] == "^2")
		{
			vec[2] = "";
			vec[4] = vec[0].toFloat()*vec[0].toFloat();
		}
		else if (vec[1] == "r")
		{
			vec[2] = "";
			vec[4] = qSqrt(vec[0].toDouble());
		}
		else if (vec[1] == "%")
		{
			vec[2] = "";
			vec[4] = vec[0].toDouble() / 100;
		}
		else if (vec[1] == "neg")
		{
			vec[2] = "";
			vec[4] = vec[0].toDouble()*-1;
		}
		//结果显示到下面的框
		QString str = vec[4].toString();
		str = QString::number(str.toDouble(), 'f', 4);
		ui.line_down->setText(str);
	}
	//上面的框显示表达式
	ui.line_up->clear();
	for (auto var : vec) {
		qInfo() << var.toString();
		QString str = var.toString();
		if (str >= '0'&& str <= '9') {
			str = QString::number(str.toDouble(), 'f', 2);
		}
		ui.line_up->insert(str);
	}
	preBtn = name.toUtf8().constData(); 
}
